﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Threading;
using Lapas.Common;

//////////////////////////////////////////////////////
//[20071222]    [VanVo]     Bổ sung ấn Shift+- sẽ hiển thị 00
//////////////////////////////////////////////////////

namespace Lapas.Controls
{
    public class StockTextBox : TextBox
    {
        // Fields
        private int a;
        private decimal b;
        private bool c;
        private bool d;
        //private EventHandler e;
        private CultureInfo cultureInfo;
        private bool customCulture;

        // Events
      //  public event EventHandler NumericValueChanged;

        // Methods
        public StockTextBox()
        {
            base.TextAlign = HorizontalAlignment.Right;
            this.a = 0;
            this.c = true;
            this.customCulture = false;
            //cultureInfo = new CultureInfo("en-US");
            cultureInfo = Thread.CurrentThread.CurrentCulture;
            // Tối đa 25 ký tự
            this.MaxLength = 25;
         
        }

      
        private string formatString()
        {
            string format = "#,##0";
            int a = this.a;
            if (a > 0)
            {
                format = format + ".";
            }
            while (a-- > 0)
            {
                format = format + "0";
            }
            //return Convert.ToDecimal(this.Text).ToString(format,cultureInfo);
            return b.ToString(format,CustomCultureInfo);
        }

        //----- them thuoc tinh gia tri canh bao 

        private decimal _valueAlarm = AppSettings.MaxmimunStockTextBoxValue;

        public decimal ValueAlarm
        {
            get { return _valueAlarm; }
            set { _valueAlarm = value; }
        }
	

	

        public bool CustomCulture
        {
            get
            {
                return customCulture;
            }
            set
            {
                customCulture = value;
            }
        }

        public CultureInfo CustomCultureInfo
        {
            get
            {
                return cultureInfo;
            }
            set
            {
                if (customCulture)
                {
                    cultureInfo = value;
                }
                else
                {
                    cultureInfo = Thread.CurrentThread.CurrentCulture;
                }
            }
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Delete:
                    if (base.SelectionStart != this.Text.IndexOf(this.DecimalSeperator))
                    {
                        if (this.SelectionLength != this.Text.Length)
                        {
                            break;
                        }
                        this.Text = "0";
                        base.SelectionStart = 1;
                        e.Handled = true;
                        return;
                    }
                    base.SelectionStart = this.Text.IndexOf(this.DecimalSeperator) + 1;
                    e.Handled = true;
                    return;

                case Keys.D0:
                case Keys.NumPad0:
                    if (!e.Control)
                    {
                        if (e.Shift)
                        {
                            //Thien sua de gioi han Text la 26 khi su dung su kien onkeydown 
                            if (this.Text.Length <= MaxLength - 3)
                            {
                                this.Text = this.Text + "000"; // Text cua TextBox
                                if (this.Text.IndexOf(this.DecimalSeperator) > 0)
                                {
                                    base.SelectionStart = this.Text.IndexOf(this.DecimalSeperator);
                                }
                                else
                                {
                                    base.SelectionStart = this.Text.Length;
                                }
                            }
                        }
                        break;
                    }
                    //Thien sua de gioi han Text la 26 khi su dung su kien onkeydown 
                    if (this.Text.Length <= MaxLength - 6)
                    {
                        this.Text = this.Text + "000000";
                        if (this.Text.IndexOf(this.DecimalSeperator) > 0)
                        {
                            base.SelectionStart = this.Text.IndexOf(this.DecimalSeperator);
                        }
                        else
                        {
                            base.SelectionStart = this.Text.Length;
                        }
                    }
                    break;
                case Keys.OemMinus:
                case Keys.Subtract:
                    if (e.Shift)
                    {
                        if (this.Text.Length <= MaxLength - 2)
                        {
                            this.Text = this.Text + "00"; // Text cua TextBox
                            if (this.Text.IndexOf(this.DecimalSeperator) > 0)
                            {
                                base.SelectionStart = this.Text.IndexOf(this.DecimalSeperator);
                            }
                            else
                            {
                                base.SelectionStart = this.Text.Length;
                            }
                        }
                    }

                    break;
                case Keys.Insert:
                    //Thien sua de gioi han Text la 26 khi su dung su kien onkeydown 
                    if (this.Text.Length <= MaxLength - 3)
                    {
                        this.Text = this.Text + "000"; // Text cua TextBox
                        if (this.Text.IndexOf(this.DecimalSeperator) > 0)
                        {
                            base.SelectionStart = this.Text.IndexOf(this.DecimalSeperator);
                        }
                        else
                        {
                            base.SelectionStart = this.Text.Length;
                        }
                    }

                    break;
                //case Keys.F12:
                //    if (e.Control && e.Shift)
                //    {
                //      //  new a().a(this, "NumericBox v0.9");
                //    }
                //    break;
            }

            base.OnKeyDown(e);
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            int selectionStart = 0;

            if (e.KeyChar == '-')
            {
                if (this.c)
                {
                    if (this.Text.IndexOf('-') < 0)
                    {
                        if (Convert.ToDecimal(this.Text) != 0M)
                        {
                            selectionStart = base.SelectionStart;
                            this.Text = "-" + this.Text;
                            base.SelectionStart = selectionStart + 1;
                            e.Handled = true;
                            return;
                        }
                    }
                    else
                    {
                        this.Text = this.Text.Substring(1);
                    }
                }
                e.Handled = true;
            }
            else if (e.KeyChar == '+')
            {
                if (this.Text.IndexOf('-') >= 0)
                {
                    this.Text = this.Text.Substring(1);
                }
                e.Handled = true;
            }
            else if (e.KeyChar == '\b')
            {
                if ((this.Text.IndexOf(this.DecimalSeperator) > 0) && (base.SelectionStart == (this.Text.IndexOf(this.DecimalSeperator) + 1)))
                {
                    base.SelectionStart = this.Text.IndexOf(this.DecimalSeperator);
                    e.Handled = true;
                }
                if (this.SelectionLength == this.Text.Length)
                {
                    this.Text = "0";
                    base.SelectionStart = 1;
                    e.Handled = true;
                }
            }
            else if (("0123456789" + this.DecimalSeperator).IndexOf(e.KeyChar) < 0)
            {
                //Van Vo xu ly dau cham
                if (e.KeyChar == '.')
                {
                    e.KeyChar = this.DecimalSeperator.ToCharArray()[0];
                    if (base.SelectionStart < (this.Text.IndexOf(this.DecimalSeperator) + 1))
                    {
                        base.SelectionStart = this.Text.IndexOf(this.DecimalSeperator) + 1;
                    }
                }
                    e.Handled = true;
                    
                
            }
            else if (e.KeyChar.ToString() == this.DecimalSeperator)
            {
                if (base.SelectionStart < (this.Text.IndexOf(this.DecimalSeperator) + 1))
                {
                    base.SelectionStart = this.Text.IndexOf(this.DecimalSeperator) + 1;
                }
                e.Handled = true;
            }
            else if ((this.Text.IndexOf(this.DecimalSeperator) > 0) && (base.SelectionStart == this.Text.Length))
            {
                e.Handled = true;
            }
            
            else
            {
                base.OnKeyPress(e);
            }
        }

        protected override void OnTextChanged(EventArgs e)
        {
            if (!this.d)
            {
                this.d = true;
                if (string.Empty.Equals(this.Text.Trim()))
                {
                    this.Text = "0";
                }
                if (this.Text.Substring(0, 1) == this.GroupSeperator)
                {
                    this.Text = this.Text.Substring(1);
                }
                try
                {
                    this.b = Convert.ToDecimal(this.Text,CustomCultureInfo);
                }
                catch (FormatException)
                {
                    this.b = 0M;
                    this.Text = "0";
                }
                int selectionStart = base.SelectionStart;
                string text = this.formatString();
                int index = this.Text.IndexOf(this.DecimalSeperator);
                if ((text.Length == (this.Text.Length - 1)) && ((selectionStart <= index) || (index < 0)))
                {
                    this.Text = text;
                    // VanVo modify on 20070725
                    if (selectionStart >= 1)
                    {
                        base.SelectionStart = selectionStart - 1;
                    }
                }
                else if ((text.Length == (this.Text.Length + 1)) && ((selectionStart <= index) || (index < 0)))
                {
                    this.Text = text;
                    base.SelectionStart = selectionStart + 1;
                }
                else
                {
                    this.Text = text;
                    base.SelectionStart = selectionStart;
                }
                if (this.Text == "0")
                {
                    base.SelectionStart = 1;
                    this.SelectionLength = 0;
                }
                this.d = false;
            }
        }

        // Properties
        public bool AllowNegativeNumeric
        {
            get
            {
                return this.c;
            }
            set
            {
                this.c = value;
            }
        }

        private string DecimalSeperator
        {
            get
            {
                return CustomCultureInfo.NumberFormat.NumberDecimalSeparator;
                
            }
        }

        private string GroupSeperator
        {
            get
            {
                return CustomCultureInfo.NumberFormat.NumberGroupSeparator;
                
            }
        }

        public object NumericValue
        {
            get
            {
                int i;
                decimal d;
                if (!this.CustomCulture)
                {
                    int.TryParse(this.Text, System.Globalization.NumberStyles.Any, Thread.CurrentThread.CurrentUICulture, out i);
                    if (i > 0) return i;
                    // Tra ve kieu Decimal
                    decimal.TryParse(this.Text, System.Globalization.NumberStyles.Any, Thread.CurrentThread.CurrentUICulture, out d);
                    return d;
                }

                // Truong hop CustomCulture
                int.TryParse(this.Text, System.Globalization.NumberStyles.Any, this.CustomCultureInfo, out i);
                if (i > 0) return i;
                // Tra ve kieu Decimal
                decimal.TryParse(this.Text, System.Globalization.NumberStyles.Any, this.CustomCultureInfo, out d);
                return d;

            }
        }

        public decimal DecimalValue
        {
            get
            {
                Decimal value = 0;
                try
                {
                    value = Convert.ToDecimal(NumericValue);
                }
                catch
                {
                }
                return value;
            }
        }

        //public object NumericValue
        //{
        //    get
        //    {
        //        return this.b;
        //    }
        //    set
        //    {
        //        if (value.Equals(DBNull.Value))
        //        {
        //            this.Text = Convert.ToString(0);
        //            this.b = Convert.ToDecimal(0);
        //            this.OnNumericValueChanged(new EventArgs());
        //        }
        //        else if (!value.Equals(this.b))
        //        {
        //            this.Text = Convert.ToString(value);
        //            this.b = Convert.ToDecimal(value);
        //            this.OnNumericValueChanged(new EventArgs());
        //        }
        //    }
        //}

        public int Precision
        {
            get
            {
                return this.a;
            }
            set
            {
                if (this.a < 0)
                {
                    this.a = Math.Abs(this.a);
                }
                this.a = value;
            }

        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // StockTextBox
            // 
            this.MaxLength = 25;
            this.ResumeLayout(false);
        }

        private bool  _alarm =  false; // mặc định không cảnh báo

        public bool  Alarm
        {
            get { return _alarm; }
            set { _alarm = value; }
        }



        protected override void OnValidated(EventArgs e)
        {
            if (_alarm == true)
            {
                decimal value = Convert.ToDecimal(NumericValue); // để không phải tính lại lần nữa 
                if (value >= _valueAlarm)
                {//20080611 Haids: update Message
                    //DisplayMessage.ShowWarning(string.Format("Giá trị {0} quá lớn", value.ToString("### ### ### ### ###")));
                    if (!DisplayMessage.ShowConfirm1(string.Format("Giá trị giao dịch lớn hơn {0} \nBạn có muốn nhập giao dịch này?", _valueAlarm.ToString("### ### ### ### ###"))))
                    {
                        // 20090720 TriDM - bỏ đoạn code gán về 0 theo Issue 1349 this.Text = "0";
                        this.Select();
                        return;
                    }
                }
            }
            base.OnValidated(e);
        }

     
    }
}
